package com.tencent.qcloud.timchat.model;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.WebViewPager;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.widget.StarDialog;
import com.tencent.TIMCustomElem;
import com.tencent.TIMMessage;
import com.tencent.qcloud.timchat.adapters.ChatAdapter;
import com.tencent.qcloud.timchat.ui.ChatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * 自定义消息
 */
public class CustomMessage extends Message {


    private String TAG = getClass().getSimpleName();
    private final int TYPE_TYPING = 14;
    Type type;
    private CustomItem item;
    JSONObject jsonObj;
    private String customIMType;


    public CustomMessage(TIMMessage message) {
        this.message = message;
        TIMCustomElem elem = (TIMCustomElem) message.getElement(0);
        parse(elem.getData());
    }




    private void parse(byte[] data) {
        type = Type.INVALID;
        try {
            String str = new String(data, "UTF-8");
            jsonObj = new JSONObject(str);
            item = new CustomItem();

            customIMType = jsonObj.getString("CustomIMType");
            item.CustomIMType = jsonObj.getString("CustomIMType");
            if (jsonObj.getString("CustomIMType").equals("produce")) {
                item.title = jsonObj.getString("p_name");
                item.content = jsonObj.getString("p_short_feature");
                item.url = jsonObj.getString("url");
                item.img = jsonObj.getString("p_img");

            } else if (jsonObj.getString("CustomIMType").equals("plan")) {
                item.title = jsonObj.getString("planname");
                item.content = jsonObj.getString("time");
                item.url = jsonObj.getString("url");
                item.img = jsonObj.getString("img");

            } else if (jsonObj.getString("CustomIMType").equals("read")) {
                item.title = jsonObj.getString("a_title");
                item.content = jsonObj.getString("a_short_content");
                item.url = jsonObj.getString("url");
                item.img = jsonObj.getString("a_image");

            }else if (jsonObj.getString("CustomIMType").equals("invite")) {
                if (message.getCustomInt() == 1) {
                    item.setContent("已经评价过了");
                } else {
                    item.setContent("未评价");
                }
                item.agentLevel = jsonObj.getString("agentLevel");
                item.inviteId = jsonObj.getString("inviteId");
                item.avatar = jsonObj.getString("avatar");
                item.name = jsonObj.getString("name");
               item.agentId = jsonObj.getString("agentId");
                item.isRealName = jsonObj.getBoolean("isRealName");
            }
            else if (jsonObj.getString("CustomIMType").equals("evaluate")) {
                item.title = jsonObj.getString("CustomIMType");
                item.score = jsonObj.getString("score");
                item.inviteId = jsonObj.getString("inviteId");
                item.agentId = jsonObj.getString("agentId");
                item.other_avatar = jsonObj.getString("other_avatar");
                item.other_name = jsonObj.getString("other_name");
            }


        } catch (IOException | JSONException e) {
            Log.e(TAG, "parse json error");
            item.CustomIMType = "";

        }
    }

    /**
     * 显示消息
     *
     * @param viewHolder 界面样式
     * @param context    显示消息的上下文
     */
    @Override
    public void showMessage(final ChatAdapter.ViewHolder viewHolder, final Context context) {
        clearView(viewHolder);
        viewHolder.message_no_read_count.setVisibility(View.GONE);
        View v;
                if(isSelf()){
                    Glide.with(context).load(LoginInfo.getInstance(context).getUser().getUe_avatar()).error(R.drawable.empty_avatar).into(viewHolder.rightAvatar);
                }else {
                    Glide.with(context).load(viewHolder.toShowUrl).error(R.drawable.empty_avatar).into(viewHolder.leftAvatar);
                }
        if (item.CustomIMType .equals("read")|| item.CustomIMType .equals("plan")||item.CustomIMType.equals ("produce")) {
//
            v =  View.inflate(context, R.layout.im_item_all, null);
            TextView title = (TextView) v.findViewById(R.id.title);
            final TextView content = (TextView) v.findViewById(R.id.content);
            final ImageView iv = (ImageView) v.findViewById(R.id.iv);
            title.setVisibility(item.getTitle().isEmpty()? View.GONE:View.VISIBLE);
            title.setText(item.getTitle());
            content.setVisibility(item.getContent().isEmpty()? View.GONE:View.VISIBLE);
            content.setText(item.getContent());
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WebViewPager.class);
                    intent.putExtra("startUrl",
                            item.getUrl());
                    context.startActivity(intent);
                }
            });

            if (item.getImg() == null) {
                iv.setVisibility(View.GONE);
            } else {
                iv.setVisibility(View.VISIBLE);
                GlideUtil.loadCircle(context,item.getImg(),iv);
            }

            getBubbleView(viewHolder).addView(v);
        } else if (item.CustomIMType.equals("invite") || item.CustomIMType.equals("evaluate")) {

            if (item.CustomIMType.equals("invite")) {
                viewHolder.leftMessage.setBackground(context.getResources().getDrawable(R.drawable.bubble_left_yellow));
                v = View.inflate(context, R.layout.im_item_invite, null);
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //todo 弹窗
                        if (message.getCustomInt() == 1)
                            return;
                        StarDialog dialog = new StarDialog((ChatActivity) context,item,CustomMessage.this);
                        dialog.show();
                    }
                });
                final TextView content = (TextView) v.findViewById(R.id.content);
                item.setContent(message.getCustomInt() == 1?"已经评价过":"未评价");
                content.setText(item.getContent());
                getBubbleView(viewHolder).addView(v);

            } else {
                viewHolder.rightMessage.setBackground(context.getResources().getDrawable(R.drawable.bubble_right_yellow));
                v = View.inflate(context, R.layout.im_item_evaluate, null);

                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        StarDialog dialog = new StarDialog((ChatActivity) context,item,CustomMessage.this);
                        dialog.show();
                    }
                });
                getBubbleView(viewHolder).addView(v);
            }


        } else {
            v = new TextView(context);
            ((TextView)v).setText("消息类型异常");
            getBubbleView(viewHolder).addView(v);
        }

        showStatus(viewHolder);
    }

    /**
     * 获取消息摘要
     */
    @Override
    public String getSummary() {
        String summary  ="";
        switch (customIMType) {
            case "produce":
                summary = "[产品]";
            break;
            case "plan":
                summary = "[计划书]";
                break;
            case "read":
                summary = "[微阅读]";
                break;
            case "evaluate":
                summary = "[评分结果]";
                break;
            case "invite":
                summary = "[评分邀请]";
                break;
        }

        return summary;
    }

    /**
     * 保存消息或消息文件
     */
    @Override
    public void save() {

    }

    public enum Type {
        TYPING,
        INVALID,
    }


}
